﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 指定文件路径
        string filePath = @"D:\Cshape2024\CShapeHomework\random_data.txt";

        // 确保文件夹存在
        string directoryPath = Path.GetDirectoryName(filePath);
        if (!Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
        }

        // 创建一个 Random 对象
        Random random = new Random();

        // 生成 10000 行内容
        List<string> lines = Enumerable.Range(1, 10000)
            .Select(i => GenerateLine(i, random))
            .ToList();

        // 将内容写入文件
        File.WriteAllLines(filePath, lines);

        // 读取文件内容并计算统计数据
        var data = File.ReadAllLines(filePath)
            .Select(line => line.Split(','))
            .Select(parts => new
            {
                Id = int.Parse(parts[0]),
                Name = parts[1],
                Age = int.Parse(parts[2]),
                Height = int.Parse(parts[3]),
                Gender = int.Parse(parts[4])
            })
            .ToList();

        // 计算统计数据
        int countFemale = data.Count(d => d.Gender == 0);
        int countMale = data.Count(d => d.Gender == 1);
        double averageAgeFemale = data.Where(d => d.Gender == 0).Average(d => d.Age);
        double averageHeightFemale = data.Where(d => d.Gender == 0).Average(d => d.Height);
        double averageAgeMale = data.Where(d => d.Gender == 1).Average(d => d.Age);
        double averageHeightMale = data.Where(d => d.Gender == 1).Average(d => d.Height);
        double averageAgeTotal = data.Average(d => d.Age);
        double averageHeightTotal = data.Average(d => d.Height);

        // 输出完成信息和统计结果
        Console.WriteLine($"10000 行内容已生成并保存到文件: {filePath}");
        Console.WriteLine($"女性数据数量: {countFemale}");
        Console.WriteLine($"男性数据数量: {countMale}");
        Console.WriteLine($"女性年龄的平均值: {averageAgeFemale:F2}");
        Console.WriteLine($"女性身高的平均值: {averageHeightFemale:F2}");
        Console.WriteLine($"男性年龄的平均值: {averageAgeMale:F2}");
        Console.WriteLine($"男性身高的平均值: {averageHeightMale:F2}");
        Console.WriteLine($"总体年龄的平均值: {averageAgeTotal:F2}");
        Console.WriteLine($"总体身高的平均值: {averageHeightTotal:F2}");
    }

    // 生成一行内容
    static string GenerateLine(int id, Random random)
    {
        // 生成 3-8 位的字母组合，首字母大写
        string letters = GenerateRandomLetters(random, 3, 8);
        string capitalizedLetters = char.ToUpper(letters[0]) + letters.Substring(1);

        // 生成服从正态分布的整数，均值为 40，标准差为 10
        int age = (int)Math.Round(GenerateNormalRandom(40, 10, random));
        age = Math.Max(20, Math.Min(60, age)); // 确保在 20 到 60 之间

        // 生成服从正态分布的整数，均值为 160，标准差为 20
        int height = (int)Math.Round(GenerateNormalRandom(160, 20, random));
        height = Math.Max(120, Math.Min(200, height)); // 确保在 120 到 200 之间

        // 生成 0 或 1 的数字
        int gender = random.Next(2); // 0 或 1

        // 构建一行内容
        return $"{id},{capitalizedLetters},{age},{height},{gender}";
    }

    // 生成 3-8 位的字母组合
    static string GenerateRandomLetters(Random random, int minLen, int maxLen)
    {
        int length = random.Next(minLen, maxLen + 1);
        const string chars = "abcdefghijklmnopqrstuvwxyz";
        return new string(Enumerable.Repeat(chars, length)
          .Select(s => s[random.Next(s.Length)]).ToArray());
    }

    // 生成服从正态分布的随机数
    static double GenerateNormalRandom(double mean, double stdDev, Random random)
    {
        double u1 = random.NextDouble(); // 随机生成 [0, 1) 之间的数
        double u2 = random.NextDouble(); // 随机生成 [0, 1) 之间的数
        double z0 = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Cos(2.0 * Math.PI * u2);
        return mean + stdDev * z0;
    }
}